package serializable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;

public class FileSerializable {
	public static void serializableToFile(Object o){
		FileOutputStream out = null;
		try{
			File f = new File("G://a.txt");
			out = new FileOutputStream(f);
			ObjectOutputStream oos = new ObjectOutputStream(out);
			oos.writeObject(o);
			oos.flush();
			out.flush();
			oos.close();
			out.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public static Object inverseSerializable(File f){
		try {
			FileInputStream in = new FileInputStream(f);
			ObjectInputStream ois = new ObjectInputStream(in);
			Object o = ois.readObject();
			return o;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("error");
		}
	}
	public static void main(String args[]){
		serializableToFile(new Cat());
		Object o = inverseSerializable(new File("G://a.txt"));
		for(Field f:o.getClass().getDeclaredFields()){
			System.out.println(f.getName());
		}
	}

}
